package com.uphone.kingmall.dialog;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SizeUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.adapter.ShopsCartPwAdapter;
import com.uphone.kingmall.bean.GoodsDetailBean;
import com.uphone.kingmall.listener.OnItemClickListener2;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.GlideUtils;
import com.uphone.kingmall.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 购物车弹�?
 */

public class ShopCartPw extends PopupWindow {
    Context context;
    EditText textViewMsg;
    GoodsDetailBean.GoodsBean bean;

    ShopsCartPwAdapter adapter1;
    Double prcie = 0.0;
    Double prcie2 = 0.0;
    List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> propList = new ArrayList<>();
    List<GoodsDetailBean.GoodsBean.PropsBean> propsList = new ArrayList<>();
    public View popupView;
    private boolean isShowNum = true;
    //    private int parentPosition = -1;
//    private int childPosition = -1;

    public ShopCartPw(final Context context, boolean isShowNum, final GoodsDetailBean.GoodsBean bean, List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> goodPropList, final setOnDialogClickListener onDialogClick, final PopupWindow.OnDismissListener setOnDialogDissmissListener) {
        super(context);
        this.isShowNum = isShowNum;
        this.context = context;
        this.bean = bean;
        if (goodPropList == null) {
            goodPropList = new ArrayList<>();
        }
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(R.layout.kingmall_shop_cart_popu, null);
        RecyclerView shopPopuRv = (RecyclerView) popupView.findViewById(R.id.shop_cart_popu_rv);
        View top_view = (View) popupView.findViewById(R.id.top_view);
        LinearLayout top_ll = (LinearLayout) popupView.findViewById(R.id.top_ll);
        TextView pw_cart_btn = (TextView) popupView.findViewById(R.id.pw_cart_btn);
        ImageView pw_goods_pic = (ImageView) popupView.findViewById(R.id.pw_goods_pic);
        final TextView pw_cart_price = (TextView) popupView.findViewById(R.id.pw_cart_price);
        TextView pw_cart_num = (TextView) popupView.findViewById(R.id.pw_cart_num);
        TextView btn_reduce = (TextView) popupView.findViewById(R.id.btn_reduce);
        final TextView tv_count = (TextView) popupView.findViewById(R.id.tv_count);
        TextView btn_add = (TextView) popupView.findViewById(R.id.btn_add);
        popupView.findViewById(R.id.iv_colse).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        popupView.findViewById(R.id.ll_other).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        View ll_num = popupView.findViewById(R.id.ll_num);
        if (bean.getGoodsType() == 3 || bean.getGoodsType() == 4 || !isShowNum) {
            ll_num.setVisibility(View.INVISIBLE);
        } else {
            ll_num.setVisibility(View.VISIBLE);
        }
        final View ll_prop = popupView.findViewById(R.id.ll_prop);
        if (bean.getProps() == null || bean.getProps().size() == 0) {
            ll_prop.setVisibility(View.GONE);
        } else {
            ll_prop.setVisibility(View.VISIBLE);
            ll_prop.post(new Runnable() {
                @Override
                public void run() {
                    if (ll_prop.getMeasuredHeight() > SizeUtils.dp2px(250)) {
                        ViewGroup.LayoutParams params = ll_prop.getLayoutParams();
                        params.height = SizeUtils.dp2px(250);
                        ll_prop.setLayoutParams(params);
                    }
                }
            });

        }
        GlideUtils.getInstance().loadCornerImage(context, bean.getGoodsMainImg(), SizeUtils.dp2px(2), pw_goods_pic);
        pw_cart_price.setText("¥" + bean.getBasePrice() + "");
        if (bean.getGoodsType() != 3 && bean.getGoodsType() != 4 && bean.getRepertoryTim() > 0) {
            pw_cart_num.setVisibility(View.VISIBLE);
            pw_cart_num.setText("每人限购" + bean.getRepertoryTim() + "件");
        } else {
            pw_cart_num.setVisibility(View.GONE);
        }
        adapter1 = new ShopsCartPwAdapter(context, bean.getProps());
        shopPopuRv.setLayoutManager(new LinearLayoutManager(context));
        shopPopuRv.setAdapter(adapter1);
        for (int i = 0; i < bean.getProps().size(); i++) {
            for (int j = 0; j < bean.getProps().get(i).getProp().size(); j++) {
                bean.getProps().get(i).getProp().get(j).setSelect(0);
                for (int k = 0; k < goodPropList.size(); k++) {
                    if (goodPropList.get(k).getGoodsPropId() == bean.getProps().get(i).getProp().get(j).getGoodsPropId()) {
                        bean.getProps().get(i).getProp().get(j).setSelect(1);
                    }
                }
            }
        }
        prcie = Double.valueOf(bean.getBasePrice());
        prcie2 = 0.0;
        for (int i = 0; i < bean.getProps().size(); i++) {
            for (int j = 0; j < bean.getProps().get(i).getProp().size(); j++) {
                if (bean.getProps().get(i).getProp().get(j).isSelect() == 1) {
                    prcie2 += Double.valueOf(bean.getProps().get(i).getProp().get(j).getAddFee());
                }
            }
        }
        pw_cart_price.setText("¥" + (prcie2 + prcie) + "");
        adapter1.setOnItemClickListener2(new OnItemClickListener2() {
            @Override
            public void onItemClick(View v, int position1, int position2) {
                prcie = Double.valueOf(bean.getBasePrice());
                prcie2 = 0.0;
                for (int i = 0; i < bean.getProps().get(position1).getProp().size(); i++) {
                    if (i == position2) {
                        bean.getProps().get(position1).getProp().get(position2).setSelect(1);
                    } else {
                        bean.getProps().get(position1).getProp().get(i).setSelect(0);
                    }
                }
                for (int i = 0; i < bean.getProps().size(); i++) {
                    for (int j = 0; j < bean.getProps().get(i).getProp().size(); j++) {
                        if (bean.getProps().get(i).getProp().get(j).isSelect() == 1) {
                            prcie2 += Double.valueOf(bean.getProps().get(i).getProp().get(j).getAddFee());
                        }
                    }
                }
                pw_cart_price.setText("¥" + (prcie2 + prcie) + "");
//                adapter1.notifyDataSetChanged();
            }
        });


        top_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//--
                dismiss();
            }
        });
        top_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//--
            }
        });
        btn_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//--
                if (Integer.valueOf(tv_count.getText().toString()) > 1) {
                    tv_count.setText(Integer.valueOf(tv_count.getText().toString()) - 1 + "");
                }
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//++
                int count = Integer.valueOf(tv_count.getText().toString()) + 1;
                if (bean.getRepertoryTim() > 0 && count > bean.getRepertoryTim()) {
                    ToastUtils.showShortToast(context, "限购" + bean.getRepertoryTim() + "件");
                } else if (bean.getRepertoryNum() >= 0 && count > bean.getRepertoryNum()) {
                    ToastUtils.showShortToast(context, "库存不足");
                } else {
                    tv_count.setText(Integer.valueOf(tv_count.getText().toString()) + 1 + "");
                }
//                onDialogClick.onClick(v, 0);
            }
        });

        pw_cart_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//确定
                if (CommonUtil.checkLogin()) {
                    propList.clear();
                    List<String> lists = new ArrayList<>();
                    for (int i = 0; i < bean.getProps().size(); i++) {
                        for (int j = 0; j < bean.getProps().get(i).getProp().size(); j++) {
                            if (bean.getProps().get(i).getProp().get(j).isSelect() == 1) {
                                propsList.add(bean.getProps().get(i));
                                propList.add(bean.getProps().get(i).getProp().get(j));
                                lists.add(bean.getProps().get(i).getProp() + "-" + bean.getProps().get(i).getProp().get(j).getGoodsPropId()
                                        + "-" + bean.getProps().get(i).getProp().get(j).getGoodsPropVal()
                                        + "-" + bean.getProps().get(i).getProp().get(j).getAddFee());
                            }
                        }
                    }
                    if (bean.getProps().size() == 0) {
                        prcie = Double.valueOf(bean.getBasePrice());
                        prcie2 = 0.0;
                    } else if (propList.size() == 0 || propList.size() < bean.getProps().size()) {
                        ToastUtils.showShortToast(context, "请选择商品规格");
                        return;
                    }
                    Log.e("购物车count", lists.toString());
                    onDialogClick.onClick(v, 666, lists.toString().substring(1, lists.toString().length() - 1), tv_count.getText().toString(), prcie2 + prcie + "");
                    onDialogClick.onClick(v, 666, propsList, propList, tv_count.getText().toString(), prcie2 + prcie + "");
                }
                dismiss();
            }
        });
        this.setContentView(popupView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点�?
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x50000000);
        // 设置SelectPicPopupWindow弹出窗体的背�?
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(setOnDialogDissmissListener);
//        this.setAnimationStyle(R.style.pupopWindowAnimation);
    }

    public ShopCartPw(final Context context, final GoodsDetailBean.GoodsBean bean, List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> goodPropList, final setOnDialogClickListener onDialogClick, final PopupWindow.OnDismissListener setOnDialogDissmissListener) {
        this(context, true, bean, goodPropList, onDialogClick, setOnDialogDissmissListener);
    }

    public interface setOnDialogClickListener {
        void onClick(View v, int type, String pos, String num, String price);

        void onClick(View v, int type, List<GoodsDetailBean.GoodsBean.PropsBean> propsBean, List<GoodsDetailBean.GoodsBean.PropsBean.PropBean> propList, String num, String price);
    }
//    public void clearCart(){
//        list.clear();
//        adapter.notifyDataSetChanged();
//    }
//    public void refreshCart(List<OpenShopCartBean.DataBean.CarGoodssBean> list){
//        this.list=list;
//        adapter.notifyDataSetChanged();
//    }

    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }
}
